<?php 
class ControllerAccountCreate extends Controller {
	private $error = array();
	      
  	public function index() {
		if ($this->customer->isLogged()) {
	  		$this->redirect(HTTPS_SERVER . 'index.php?route=account/account');
    	}

    	$this->language->load('account/create');
		
		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('account/customer');
		$this->load->model('tool/seo_url');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_account_customer->addCustomer($this->request->post);

			unset($this->session->data['guest']);

			$this->customer->login($this->request->post['email'], $this->request->post['password']);
									
			$this->language->load('mail/account_create');
			
			$subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name'));
			
			$message = sprintf($this->language->get('text_welcome'), $this->config->get('config_name')) . "\n\n";
			

			$message .= $this->language->get('text_login') . "\n";

			
			$message .= HTTPS_SERVER . 'index.php?route=account/login' . "\n\n";
			$message .= $this->language->get('text_services') . "\n\n";
			$message .= $this->language->get('text_thanks') . "\n";
			$message .= $this->config->get('config_name');
			
			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');				
			$mail->setTo($this->request->post['email']);
	  		$mail->setFrom($this->config->get('config_email'));
	  		$mail->setSender($this->config->get('config_name'));
	  		$mail->setSubject($subject);
			$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
      		$mail->send();
	  	  
	  		$this->redirect(HTTPS_SERVER . 'index.php?route=account/success');
    	} 

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => 'Trang chủ',//$this->language->get('text_home'),
        	'separator' => FALSE
      	); 

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/account',
        	'text'      => 'Thông tin cá nhân',//$this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/create',
        	'text'      => 'Đăng kí thành viên',//$this->language->get('text_create'),
        	'separator' => $this->language->get('text_separator')
      	);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['home_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');
		$this->data['register_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=account/create');
		
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select'] = $this->language->get('text_select');
    	$this->data['text_account_already'] = sprintf($this->language->get('text_account_already'), HTTPS_SERVER . 'index.php?route=account/login');
    	$this->data['text_your_details'] = $this->language->get('text_your_details');
    	$this->data['text_your_address'] = $this->language->get('text_your_address');
    	$this->data['text_your_password'] = $this->language->get('text_your_password');
		$this->data['text_newsletter'] = $this->language->get('text_newsletter');
				
    	$this->data['entry_firstname'] = $this->language->get('entry_firstname');
    	$this->data['entry_lastname'] = $this->language->get('entry_lastname');
    	$this->data['entry_email'] = $this->language->get('entry_email');
    	$this->data['entry_telephone'] = $this->language->get('entry_telephone');
    	$this->data['entry_fax'] = $this->language->get('entry_fax');
    	$this->data['entry_company'] = $this->language->get('entry_company');
    	$this->data['entry_address_1'] = $this->language->get('entry_address_1');
    	$this->data['entry_address_2'] = $this->language->get('entry_address_2');
    	$this->data['entry_postcode'] = $this->language->get('entry_postcode');
    	$this->data['entry_city'] = $this->language->get('entry_city');
    	$this->data['entry_country'] = $this->language->get('entry_country');
    	$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['entry_password'] = $this->language->get('entry_password');
    	$this->data['entry_confirm'] = $this->language->get('entry_confirm');

		$this->data['button_continue'] = $this->language->get('button_continue');
    
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}	
		
	
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}
		
		if (isset($this->error['birth'])) {
			$this->data['error_birth'] = $this->error['birth'];
		} else {
			$this->data['error_birth'] = '';
		}
		
		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}
		
 		if (isset($this->error['confirm'])) {
			$this->data['error_confirm'] = $this->error['confirm'];
		} else {
			$this->data['error_confirm'] = '';
		}
		
  	 	if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}   		
		
    	$this->data['action'] = HTTPS_SERVER . 'index.php?route=account/create';

		if (isset($this->request->post['name'])) {
    		$this->data['name'] = $this->request->post['name'];
		} else {
			$this->data['name'] = '';
		}

		if (isset($this->request->post['email'])) {
    		$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = '';
		}

  		if (isset($this->request->post['sex'])) {
    		$this->data['sex'] = $this->request->post['sex'];
		} else {
			$this->data['sex'] = '-1';
		}		

  	  	if (isset($this->request->post['birthday'])) {
    		$this->data['birthday'] = $this->request->post['birthday'];
		} else {
			$this->data['birthday'] = '-1';
		}

  	  	if (isset($this->request->post['birthmonth'])) {
    		$this->data['birthmonth'] = $this->request->post['birthmonth'];
		} else {
			$this->data['birthmonth'] = '-1';
		}

  	  	if (isset($this->request->post['birthyear'])) {
    		$this->data['birthyear'] = $this->request->post['birthyear'];
		} else {
			$this->data['birthyear'] = '-1';
		}
				
		if (isset($this->request->post['telephone'])) {
    		$this->data['telephone'] = $this->request->post['telephone'];
		} else {
			$this->data['telephone'] = '';
		}
		
		if (isset($this->request->post['mobile'])) {
    		$this->data['mobile'] = $this->request->post['mobile'];
		} else {
			$this->data['mobile'] = '';
		}
		
		
		if (isset($this->request->post['address'])) {
    		$this->data['address'] = $this->request->post['address'];
		} else {
			$this->data['address'] = '';
		}

  		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}
				
		if (isset($this->request->post['password'])) {
    		$this->data['password'] = $this->request->post['password'];
		} else {
			$this->data['password'] = '';
		}
		
		if (isset($this->request->post['confirm'])) {
    		$this->data['confirm'] = $this->request->post['confirm'];
		} else {
			$this->data['confirm'] = '';
		}
		

		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info) {
				$this->data['text_agree'] = sprintf($this->language->get('text_agree'), HTTP_SERVER . 'index.php?route=information/information&information_id=' . $this->config->get('config_account_id'), $information_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}
		
		if (isset($this->request->post['agree'])) {
      		$this->data['agree'] = $this->request->post['agree'];
		} else {
			$this->data['agree'] = FALSE;
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/create.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/create.tpl';
		} else {
			$this->template = 'default/template/account/create.tpl';
		}
		
		$this->children = array(
			'common/column_right',
			'common/footer',
			'common/column_left',
			'common/header',
			'common/menu_content'
		);

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));	
  	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	  	
  	private function validate() {
    	if ((strlen(utf8_decode($this->request->post['name'])) < 1) || (strlen(utf8_decode($this->request->post['name'])) > 32)) {
      		//$this->error['name'] = $this->language->get('error_firstname');
      		$this->error['name'] = "Họ tên phải có ít nhất 1 kí tự và nhiều nhất 32 kí tự.";
    	}

    	if ((strlen(utf8_decode($this->request->post['email'])) > 96) || (!preg_match(EMAIL_PATTERN, $this->request->post['email']))) {
      		//$this->error['email'] = $this->language->get('error_email');
      		$this->error['email'] = "Email không đúng định dạng.";
    	}

    	if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
      		//$this->error['warning'] = $this->language->get('error_exists');
      		$this->error['warning'] = "Email đã được dùng để đăng kí.";
    	}
		
    	if ((strlen(utf8_decode($this->request->post['password'])) < 4) || (strlen(utf8_decode($this->request->post['password'])) > 20)) {
      		//$this->error['password'] = $this->language->get('error_password');
      		$this->error['password'] = "Mật khẩu phải có ít nhất 4 kí tự và nhiều nhất 20 kí tự.";
    	}

    	if ($this->request->post['confirm'] != $this->request->post['password']) {
      		//$this->error['confirm'] = $this->language->get('error_confirm');
      		$this->error['confirm'] = "Xác nhận mật khẩu không chính xác.";
    	}
  	    
  	    if ($this->request->post['birthday'] == 0 ||  $this->request->post['birthmonth'] == 0 ||  $this->request->post['birthyear'] == 0) {
      		//$this->error['confirm'] = $this->language->get('error_confirm');
      		$this->error['birth'] = "Phải chọn đầy đủ ngày,tháng,năm sinh.";
    	}

  	    if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		//$this->error['captcha'] = $this->language->get('error_captcha');
      		$this->error['captcha'] = "Mã xác nhận nhập chưa chính xác."; 
    	}    	
		
    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
  
}
?>